﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using Business;

namespace QuanLyNhaSach
{
    
    public partial class CapNhatTheLoai : Form
    {
       public event EventHandler CapNhatTheLoaiEventHandle;
       
       
        public CapNhatTheLoai()
        {
            InitializeComponent();
        }
        private void SetUpDataGridViewTheLoai()
        {
            dgvTheLoai.BackgroundColor = Color.White;
            dgvTheLoai.ColumnCount = 2;
            dgvTheLoai.Columns[0].Name ="Mã Thể Loại";
            dgvTheLoai.Columns[1].Name = "Tên Thể Loại";

            dgvTheLoai.Columns[0].ReadOnly = true;

            dgvTheLoai.Columns[0].Width = 80;
            dgvTheLoai.Columns[1].Width = 240;

            dgvTheLoai.AllowUserToAddRows = false;
            dgvTheLoai.AllowUserToDeleteRows = false;
        }
        private void LoadDuLieuDataGridViewTheLoai()
        {
            List<TheLoaiDTO> list = new List<TheLoaiDTO>();
            list = TheLoaiService.GetAllTheLoai();
            foreach (TheLoaiDTO theloai in list)
            {
                DataGridViewRow row = new DataGridViewRow();
                row.CreateCells(dgvTheLoai);

               
               
                row.Cells[0].Value = theloai.MaTheLoai.ToString();
                row.Cells[1].Value = theloai.TenTheLoai.ToString();
                dgvTheLoai.Rows.Add(row);
                
            }
        }


       

      

        private void dgvTheLoai_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            //MessageBox.Show("cellvaluechange");
           // MessageBox.Show(dgvTheLoai.SelectedCells[0].RowIndex.ToString());
            TheLoaiDTO theloaiDTO = new TheLoaiDTO();
            theloaiDTO.MaTheLoai =int.Parse( dgvTheLoai.Rows[dgvTheLoai.SelectedCells[0].RowIndex].Cells[0].Value.ToString());
            theloaiDTO.TenTheLoai = dgvTheLoai.Rows[dgvTheLoai.SelectedCells[0].RowIndex].Cells[1].Value.ToString();
            TheLoaiService.Update(theloaiDTO);
           
            if (CapNhatTheLoaiEventHandle != null)
            {
                CapNhatTheLoaiEventHandle(sender, e);
            } 
            
            
            
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            
            this.Close();
           

                

        }

        private void CapNhatTheLoai_Load(object sender, EventArgs e)
        {
            SetUpDataGridViewTheLoai();
            LoadDuLieuDataGridViewTheLoai();
            int n = TheLoaiService.NumberID();
            MessageBox.Show(n.ToString());
           
            
        }

        private void CapNhatTheLoai_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult r = MessageBox.Show("Bạn có muốn thoát", "Thông báo", MessageBoxButtons.YesNo);
            if(r==DialogResult.No)
                e.Cancel=true;
        }

       
    }
}
